// by Xeno
// Commented/Edited by Tyrghen
if (d_global_debug > 0) then { diag_log Format["Start initvec for %1", _this]; };

// Defines a multiline macro (hence the \ at the end) which will spawn the marker for the vehicle
// We also add the vehicle to the list of vehicle whose markers should be updated.
#define __vecmarker [_car select 2, position _vec,"ICON",_car select 4,[0.6,0.6],_mt,0,_car select 3] call XfCreateMarkerLocal;\
_vec setVariable ["d_marker", _car select 2];\
_vec setVariable ["d_marker_text", _car select 5];\
_vec setVariable ["d_marker_color", _car select 4];\
d_marker_vecs set [count d_marker_vecs, _vec];

// Defines a multiline macro (hence the \ at the end) which will spawn the marker for the chopper
#define __chopmarker [_car select 2, position _vec,"ICON",_car select 5,[0.6,0.6],_car select 6,0,_car select 4] call XfCreateMarkerLocal;\
_vec setVariable ["d_marker", _car select 2];\
_vec setVariable ["d_marker_text", _car select 6];\
_vec setVariable ["d_marker_color", _car select 5];\
d_marker_vecs set [count d_marker_vecs, _vec];\
if (count _car > 8) then {_vec setVariable ["d_lift_types", _car select 8]}

// Adds the chopper hud and stuffs
// Defines a multiline macro (hence the \ at the end)
#define __chopset _index = _car select 1;\
_vec setVariable ["d_choppertype", _index];\
_vec setVariable ["d_vec_type", "chopper"];\
_vec addEventHandler ["getin", {[_this,0] spawn x_checkhelipilot}];\
_vec addEventHandler ["getout", {_this spawn x_checkhelipilotout}]

#define __sidew _vec setVariable ["d_side", west]
#define __sidee _vec setVariable ["d_side", east]
#define __vecname _vec setVariable ["d_vec_name", _car select 6]
#define __chopname _vec setVariable ["d_vec_name", _car select 7]
// TODO : Check if we need this
//#define __checkenterer _vec addEventHandler ["getin", {_this spawn x_checkenterer}]

// Checks whether the vehicle number is in the array d_p_vecs (defined in i_client.sqf) we have a vehicle which has the same vehicle number as the number defined in init.sqf on the call of vrespawn2.sqf
#define __pvecs {if ((_x select 1) == _d_vec) exitWith {_car = _x}} forEach d_p_vecs

// Add a chopper menu to the vehicle
#define __addchopm _vec addAction [localize "MSG_DOM_VECCHOPPERMENU" call XGreyText,"client\vecdialog.sqf",[],-1,false]
// Add a halo menu to the vehicle if the vehicle is above 200m
// #define __halo _vec addAction [localize "MSG_DOM_VECHALOJUMP" call XGreyText,"client\halo.sqf",[],-1,false,true,"","vehicle player != player && ((vehicle player) call XfGetHeight) > 200"]
#define __addvecm _vec addAction [localize "MSG_DOM_VECVEHICLEMENU" call XGreyText,"client\vecdialog.sqf",[],-1,false]

private ["_vec"];

_vec = _this;

// Set in helirespawn2.sqf and vrespawn2.sqf. This is the number indicating the type of vehicle.
// If you want those two scripts to add the value, you need to add your vehicle
// in the array d_p_vecs of i_client.sqf + the array given to vrespawn2.sqf in init.sqf
_d_vec = _vec getVariable "d_vec";
if (isNil "_d_vec") exitWith {};

// We check if we already went through initvec.sqf. If it is the case, we exit.
// The reason why we do this check is that you don't know when a vehicle is added in regard to the current login state of the player.
// All vehicle markers are triggered at vehicle initialization through the call of the client event d_n_v.
// But you don't know if a player is already listening to this event. So in the SetupPlayer.sqf, we also trigger initvec
// for all vehicle which were already initialized. As it happens on both the server and the client, it's possible that
// the server and the client will try to init the same vehicle at the same time. The check below prevents that.
if (!isNil {_vec getVariable "d_vcheck"}) exitWith {};
_vec setVariable ["d_vcheck", true];

// Used to know if a player entering the vehicle should be set to captive
// This allows for making "Safe" vehicles for a player to use in special missions, like boats, etc.
_d_safe = _vec getVariable "d_safe";
if (isNil "_d_safe") then { _d_safe = false; };
If (_d_safe) then {
	_vec addEventHandler ["getin", {_unit = _this select 2; _unit setCaptive true; hintSilent "You feel safe"; diag_log Format["GetIn Set Captive: %1 - %2", _this select 0, _unit]; waituntil{(_unit == vehicle _unit || !alive _unit)}; _unit setCaptive false;}];
	_vec addEventHandler ["getout", {(_this select 2) setCaptive false; diag_log Format["GetOut Unset Captive: %1 - %2", _this select 0, _this select 2];}];
};
_vec addEventHandler ["getin", XfCheckReservedVehicle];
// MHQ Vehicles
if (_d_vec < 10) exitWith {
	_car = [];
	// This macro checks that the vehicle was defined with the same number in both i_client.sqf and in init.sqf
	// If it is the case, you'll get back the array defined in i_client for this vehicle
	__pvecs;
	// Check that we got something else than an empty value
	if (count _car > 0) then {
		// We create a global variable which the name being the first element of the i_client array.
		// in the case of the MHQ: MRR1, MRR2. And we associate the current vehicle to it.
		missionNamespace setVariable [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		// Marker text
		_mt = _car select 5;
		// We update the marker text with the Deployed status of the vehicle.
		if (!isNil {_vec getVariable "D_MHQ_Deployed"}) then {
			if (_vec getVariable "D_MHQ_Deployed") then {
				_mt = _mt + " " + localize "MSG_DOM_MHQMRKDEPLOYED";
				_vec lock true;
			};
		};
		// We check that there is a marker with the name defined in i_client.
		// in the case of the MHQ: mobilerespawn1, mobilerespawn2
		if (str(markerPos (_car select 2)) != "[0,0,0]") then {
			(_car select 2) setMarkerTextLocal _mt;
		};
		// The macro creates a local marker by all systems and adds the vehicle to the list of vehicle which should update a marker when moving.
		__vecmarker;
		// We set the variable with the base marker text of the vehicle. ex: MHQ One
		_vec setVariable ["d_marker_text", _car select 6];
		_vec setVariable ["d_marker_color", _car select 4];
		// Sets the name of the vehicle in a variable inside the object
		__vecname;
	};
	if (!alive _vec) exitWith {};
	// We add the rest of the needed values.
	_vec addeventhandler ["killed", {(_this select 0) setVariable ["D_MHQ_Deployed",false,true]}];
	_vec addAction [localize "MSG_DOM_MHQMENU" call XGreyText,"client\vecdialog.sqf",_d_vec,-1,false];
	_vec setVariable ["d_vec_type", "MHQ", true];
	_vec setAmmoCargo 0;
	_vec setVariable ["d_ammotype",true,true];
	// This part checks if the vehicle is being lifted or if the driver dies, or the lifter is destroyed or the vehicle is destroyed.
	_vec spawn {
		private ["_vehicle","_transport"];
		_vehicle = _this;
		while {alive _vehicle} do {
			waituntil {sleep 3; ((!(IsNull(_vehicle getVariable "R3F_LOG_est_transporte_par")) AND (local _vehicle)) OR (!alive _vehicle))};
			_transport = (_vehicle getVariable "R3F_LOG_est_transporte_par");
			if (!(IsNil "_transport") AND (alive _vehicle) AND (local _vehicle)) then {
				switch (_vehicle) do {
					case MRR1: {
						["mr1_in_air",true] call XNetSetJIP;
						["d_msg",[localize "MSG_DOM_MHQONEISLIFTED","all",objNull,"hq"]] call XNetCallEvent;
					};
					case MRR2: {
						["mr2_in_air",true] call XNetSetJIP;
						["d_msg",[localize "MSG_DOM_MHQTWOISLIFTED","all",objNull,"hq"]] call XNetCallEvent;
					};
				};
				waituntil {sleep 3; ((isNil "_transport") OR (isNull driver _transport) OR (!alive _vehicle))};
				if (alive _vehicle) then {
					switch (_vehicle) do {
						case MRR1: {
							["mr1_in_air",false] call XNetSetJIP;
							["d_msg",[localize "MSG_DOM_MHQONEISAVAILABLE","all",objNull,"hq"]] call XNetCallEvent;
						};
						case MRR2: {
							["mr2_in_air",false] call XNetSetJIP;
							["d_msg",[localize "MSG_DOM_MHQTWOISAVAILABLE","all",objNull,"hq"]] call XNetCallEvent;
						};	
					};
				};
			};	
		};
		// If we reach here, the MHQ is destroyed.
	};
};
if ((((typeof _vec) in d_check_ammo_load_vecs) || ((count d_check_ammo_load_vecs) <= 0)) && (_vec iskindof "LandVehicle")) then {
	_vec setVariable ["d_ammotype",true,true];
	if (!alive _vec) exitWith {};
	__addvecm;
};

// Medical vehicles
if (_d_vec < 20) exitWith {
	_car = [];
	__pvecs;
	if (count _car > 0) then {
		missionNamespace setVariable [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		_mt = _car select 5;
		__vecmarker;
		__vecname;
	};
	if (!alive _vec) exitWith {};
	_vec setAmmoCargo 0;
};

// Trucks Ammo, Repair, Fuel
if (_d_vec < 30) exitWith {
	_car = [];
	__pvecs;
	if (count _car > 0) then {
		missionNamespace setVariable [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		_mt = _car select 5;
		__vecmarker;
		__vecname;
	};
	if (!alive _vec) exitWith {};
	_vec setAmmoCargo 0;
};

// Trucks Engineer
if (_d_vec < 40) exitWith {
	_car = [];
	__pvecs;
	if (count _car > 0) then {
		missionNamespace setVariable [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		_mt = _car select 5;
		__vecmarker;
		__vecname;
	};
	if (!alive _vec) exitWith {};
	_vec setVariable ["d_vec_type", "Engineer"];
	_vec setAmmoCargo 0;
};

// Truck transport and other vehicles which only need to respawn. No special things
if (_d_vec < 100) exitWith {
	_car = [];
	__pvecs;
	if (count _car > 0) then {
		missionNamespace setVariable [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		_mt = _car select 5;
		__vecmarker;
		__vecname;
	};
	if (!alive _vec) exitWith {};
	_vec setAmmoCargo 0;
	// Adding a menu to get back into the boat when at sea, because sometimes the positions do not show
	If (_vec isKindOf "Ship") then {
		_vec addAction [localize "MSG_DOM_VECBOARDSHIP" call XGreyText,"client\boardship.sqf",[],-10,false,true,"","((surfaceIsWater [(getPos player) select 0,(getPos player) select 1]) AND (player == vehicle player))"];
	};
};

// MHQ Choppers
if (_d_vec < 110) exitWith {
	_car = [];
	// This macro checks that the vehicle was defined with the same number in both i_client.sqf and in init.sqf
	// If it is the case, you'll get back the array defined in i_client for this vehicle
	__pvecs;
	// Check that we got something else than an empty value
	if (count _car > 0) then {
		// We create a global variable which the name being the first element of the i_client array.
		// in the case of the MHQ: MRR1, MRR2. And we associate the current vehicle to it.
		missionNamespace setVariable [_car select 0, _vec];
		if (!alive _vec) exitWith {};
		// Marker text
		_mt = _car select 5;
		// We update the marker text with the Deployed status of the vehicle.
		if (!isNil {_vec getVariable "D_MHQ_Deployed"}) then {
			if (_vec getVariable "D_MHQ_Deployed") then {
				_mt = _mt + localize "MSG_DOM_MHQMRKDEPLOYED";
				_vec lock true;
			};
		};
		// We check that there is a marker with the name defined in i_client.
		// in the case of the MHQ: mobilerespawn1, mobilerespawn2
		if (str(markerPos (_car select 2)) != "[0,0,0]") then {
			(_car select 2) setMarkerTextLocal _mt;
		};
		// The macro creates a local marker by all systems and adds the vehicle to the list of vehicle which should update a marker when moving.
		__vecmarker;
		// We set the variable with the base marker text of the vehicle. ex: MHQ One
		_vec setVariable ["d_marker_text", _car select 6];
		_vec setVariable ["d_marker_color", _car select 4];
		// Sets the name of the vehicle in a variable inside the object
		__vecname;
	};
	if (!alive _vec) exitWith {};
	// We add the rest of the needed values.
	_vec addeventhandler ["killed", {(_this select 0) setVariable ["D_MHQ_Deployed",false,true]}];
	_vec addAction [localize "MSG_DOM_MHQMENU" call XGreyText,"client\vecdialog.sqf",_d_vec,-1,false];
	_vec setVariable ["d_vec_type", "MHQ"];
	_vec setAmmoCargo 0;
	
	// This part checks if the vehicle is being lifted or if the driver dies, or the lifter is destroyed or the vehicle is destroyed.
	_vec spawn {
		private ["_vehicle","_transport"];
		_vehicle = _this;
		while {alive _vehicle} do {
			waituntil {sleep 3; ((!(IsNull(_vehicle getVariable "R3F_LOG_est_transporte_par")) AND (local _vehicle)) OR (!alive _vehicle))};
			_transport = (_vehicle getVariable "R3F_LOG_est_transporte_par");
			if (!(IsNil "_transport") AND (alive _vehicle) AND (local _vehicle)) then {
				switch (_vehicle) do {
					case MRR1: {
						["mr1_in_air",true] call XNetSetJIP;
						["d_msg",[localize "MSG_DOM_MHQONEISLIFTED","all",objNull,"hq"]] call XNetCallEvent;
					};
					case MRR2: {
						["mr2_in_air",true] call XNetSetJIP;
						["d_msg",[localize "MSG_DOM_MHQTWOISLIFTED","all",objNull,"hq"]] call XNetCallEvent;
					};
				};
				waituntil {sleep 3; ((isNil "_transport") OR (isNull driver _transport) OR (!alive _vehicle))};
				if (alive _vehicle) then {
					switch (_vehicle) do {
						case MRR1: {
							["mr1_in_air",false] call XNetSetJIP;
							["d_msg",[localize "MSG_DOM_MHQONEISAVAILABLE","all",objNull,"hq"]] call XNetCallEvent;
						};
						case MRR2: {
							["mr2_in_air",false] call XNetSetJIP;
							["d_msg",[localize "MSG_DOM_MHQTWOISAVAILABLE","all",objNull,"hq"]] call XNetCallEvent;
						};	
					};
				};
			};	
		};
		// If we reach here, the MHQ is destroyed.
	};
};

if ((((typeof _vec) in d_check_ammo_load_vecs) || ((count d_check_ammo_load_vecs) <= 0)) && (_vec iskindof "Air")) then {
	_vec setVariable ["d_ammotype",true,true];
	if (!alive _vec) exitWith {};
	__addchopm;
};

// Choppers
if (_d_vec < 500) exitWith {
	_car = [];
	{if ((_x select 3) == _d_vec) exitWith {_car = _x}} forEach d_choppers;
	if (count _car > 0) then {
		if (!alive _vec) exitWith {};
		missionNamespace setVariable [_car select 0, _vec];
		__chopname;
		__chopmarker;
	};
	if (!alive _vec) exitWith {};
	__chopset;
	// Added in playersetup.sqf and playerspawn.sqf on the players themselves and activated when they are 200m above ground.
	//__halo;
};

